﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Word_Mojo
{
    /// <summary>
    /// EnglishDictionary provides services for checking a word is spelled correctly
    /// </summary>
    class EnglishDictionary
    {

        //this is to store the words in the english dictionary
        //the boolean values of this dictionary are obsolete,
        //the main purpose of this is to be able to have a O(1) search
        //time for checking if a word is in the english dictionary
        private static Dictionary<string, bool> words;
        private static Dictionary<string, bool> wordsOfLengthThree;
        private static Dictionary<string, bool> wordsOfLengthFour;

        /// <summary>
        /// 
        /// </summary>
        static EnglishDictionary()
        {
            words = new Dictionary<string, bool>();
            wordsOfLengthThree = new Dictionary<string, bool>();
            wordsOfLengthFour = new Dictionary<string, bool>();

            populateWords();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="possibleWord"></param>
        /// <returns></returns>
        public static bool IsInDictionary(string possibleWord)
        {
            return words.ContainsKey(possibleWord);   
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="possibleWord"></param>
        /// <returns></returns>
        public static bool IsInThreeLetterDictionary(string possibleWord)
        {
            return wordsOfLengthThree.ContainsKey(possibleWord);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="possibleWord"></param>
        /// <returns></returns>
        public static bool IsInFourLetterDictionary(string possibleWord)
        {
            return wordsOfLengthFour.ContainsKey(possibleWord);
        }

        #region Private Helpers

        private static void populateWords()
        {
            using (StreamReader sr = new StreamReader("words.txt"))
            {
                String word;
                while ((word = sr.ReadLine()) != null)
                {
                    switch (word.Length)
                    {
                        case 3:
                            wordsOfLengthThree.Add(word, true);
                            break;
                        case 4:
                            wordsOfLengthFour.Add(word, true);
                            break;
                        default:
                            break;
                    }

                    words.Add(word, true);
                }
            }
        }

        #endregion
    }
}
